package com.kdtech.utils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {

	    /**
	     * 简单匹配
	     *
	     * @param testString
	     *            测试字符
	     * @param regular
	     *            测试正则表达式
	     */
	    public static boolean match(final String testString,final String regular) {
	        Pattern pattern = Pattern.compile(regular);
	        Matcher marcher = pattern.matcher(testString);

	        if (marcher.find()) {
	           return true;
	        } else {
	           return false;
	        }
	    }

	    public static  boolean matchIgnoreCase(final String testString,final String regular) {
	        Pattern pattern = Pattern.compile(regular,Pattern.CASE_INSENSITIVE);
	        Matcher marcher = pattern.matcher(testString);
	        if (marcher.matches()) {
	           return true;
	        } else {
	           return false;
	        }
	    }

	public static String[] getMatchGroupStrings(final String testString, final String regular) {
		Pattern pattern = Pattern.compile(regular);
		Matcher matcher = pattern.matcher(testString);

		if (matcher.find()) {
			int groupCount = matcher.groupCount();
			String[] matchs = new String[groupCount];
			for (int i = 0; i < groupCount; i++) {
				matchs[i] = matcher.group(i + 1);
			}
			return matchs;
		} else {
			return new String[0];
		}
	}

	public static String getMatchGroupString(final String testString, final String regular , int groupIndex) {
		Pattern pattern = Pattern.compile(regular);
		Matcher matcher = pattern.matcher(testString);
		if (matcher.find() && groupIndex <=matcher.groupCount()) {
			return matcher.group(groupIndex);
		} else {
			return null;
		}
	}

	    public static void main(String[] args) {
	       /*
	         * ^ $

	        MarchRegular("abb", "^a*b$");
	        MarchRegular("abbc", "^a*b$");

	         * [ab]
	         *

	        MarchRegular("a", "[abc]");
	        MarchRegular("d", "[abc]");

	         * [^ab]

	        MarchRegular("d", "[^abc]");
	        MarchRegular("a", "[^abc]");

	         * 点号匹配任意字符

	        MarchRegular("a", ".");

	         * 多选结构

	        MarchRegular("a", "a|b|bc");
	        MarchRegular("bc", "a|b|bc");
	        MarchRegular("bd", "a|b|bc");
	        MarchRegular("d", "a|b|bc");


	         * 忽略大小写 java中忽略大小写还有另一种写法,
	         * Pattern.compile的第二个参数:Pattern.CASE_INSENSITIVE)
	         *

	        MarchRegular("d", "(?i:D)");
	        MarchRegular("D", "(?i:d)");


	         * 单词边界 \\b 这里\b是对应单词边界 \\b其中对\进行了转义

	        MarchRegular("test", "\\btest\\b");
	        MarchRegular("tes", "\\btest\\b");


	         * 可选项元素 ?

	        MarchRegular("tes", "test?");
	        MarchRegular("test", "test?");
	        MarchRegular("tesm", "test?");


	         * + *

	        MarchRegular("test", "test+");
	        MarchRegular("testt", "test+");
	        MarchRegular("tes", "test+");

	        MarchRegular("test", "test*");
	        MarchRegular("testt", "test*");
	        MarchRegular("tes", "test*");
	        MarchRegular("tess", "test*");
	        MarchRegular("tem", "test*");


	         * 区间

	        MarchRegular("m", "m{0,4}");
	        MarchRegular("mm", "m{0,4}");
	        MarchRegular("mmm", "m{0,4}");
	        MarchRegular("mmmm", "m{0,4}");
	        MarchRegular("mm", "m{3,4}");
	  */
	        /*
	         * 捕获以及反向引用 java中反向引用采用 matcher.group(arg)
	         */
//	        MarchGroup("mmaa", "(m)(a)");
//	        MarchGroup("aammmmmmmssssaa", "(.)(?=.*)");
//	        MarchGroup("<?xml version='1.0' encoding='Big5'?>", "(encoding=')(.*?)(')");

	        System.out.println(getMatchGroupString("<?xml version='1.0' encoding='Big5'?>", "(encoding=')(.*?)(')", 2));
	    }

	    public static boolean matchAnyIgnoreCase(String url, String... regex) {
			for (int i = 0; i < regex.length; i++) {
				if (matchIgnoreCase(url,regex[i])){
					return true;
				}
			}
			return false;
		}

	    public static boolean matchAnyIgnoreCase(String url,List<String> regex) {
	    	if(regex==null)return false;
			for (int i = 0; i < regex.size(); i++) {
				if (matchIgnoreCase(url,regex.get(i))){
					return true;
				}
			}
			return false;
		}

		public static boolean matchAny(String url, String[] regex) {
			for (int i = 0; i < regex.length; i++) {
				if (url.matches(regex[i])) {
						return true;
//					}
				}
			}
			return false;
		}
}

